if(CMAKE_SYSTEM_NAME STREQUAL "Windows")
    message(STATUS "Building for Windows")
endif()

if (MSVC)  # Visual Studio
    message(STATUS "==Configuring for Visual Studio==")
    add_definitions(-DNOMINMAX)

    if ("${CMAKE_GENERATOR}" MATCHES "(Win64|IA64)")
        set(TARGET_ARCH "x64")
    elseif ("${CMAKE_GENERATOR_PLATFORM}" MATCHES "ARM64")
        set(TARGET_ARCH "ARM64")
    elseif ("${CMAKE_GENERATOR}" MATCHES "ARM")
        set(TARGET_ARCH "ARM")
    elseif ("${CMAKE_SIZEOF_VOID_P}" STREQUAL "8")
        set(TARGET_ARCH "x64")
    else ()
        set(TARGET_ARCH x86)
    endif ()
elseif(CMAKE_SYSTEM_NAME STREQUAL "Linux")
    message(STATUS "==Configuring for Linux==")
    add_link_options(-Wl,--disable-new-dtags)
    if(CMAKE_SYSTEM_PROCESSOR STREQUAL "x86_64")
        set(TARGET_ARCH "x64")
    elseif(CMAKE_SYSTEM_PROCESSOR STREQUAL "aarch64")
        set(TARGET_ARCH "ARM64")
    elseif(CMAKE_SYSTEM_PROCESSOR STREQUAL "armv7l")
        set(TARGET_ARCH "ARM")
    elseif(CMAKE_SYSTEM_PROCESSOR STREQUAL "i686" OR CMAKE_SYSTEM_PROCESSOR STREQUAL "i386")
        set(TARGET_ARCH "x86")
    else()
        set(TARGET_ARCH ${CMAKE_SYSTEM_PROCESSOR})
        message(WARNING "Unknown Linux architecture: ${CMAKE_SYSTEM_PROCESSOR}, using as-is")
    endif()
elseif(CMAKE_SYSTEM_NAME STREQUAL "Android")
    message(STATUS "Configuring for Android Studio")

    # Common flags
    set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -Wl,-rpath=./")

    # Debug flags
    set(CMAKE_CXX_FLAGS_DEBUG "$ENV{CXXFLAGS} -O0 -Wall -g -ggdb")

    # Release flags (and RelWithDebInfo for Gradle)
    set(CMAKE_CXX_FLAGS_RELEASE "$ENV{CXXFLAGS} -O3")
    set(CMAKE_EXE_LINKER_FLAGS_RELEASE "${CMAKE_EXE_LINKER_FLAGS_RELEASE} -s")
    set(CMAKE_CXX_FLAGS_RELWITHDEBINFO "$ENV{CXXFLAGS} -O3")
    set(CMAKE_EXE_LINKER_FLAGS_RELWITHDEBINFO "${CMAKE_EXE_LINKER_FLAGS_RELWITHDEBINFO} -s")

    set(TARGET_ARCH ${ANDROID_ABI})
else()
    message(WARNING "Unknown system: ${CMAKE_SYSTEM_NAME}")
endif ()

message(STATUS "TARGET_ARCH: ${TARGET_ARCH}")